<template>
  <div class="choose-course">
    <div class="table-data">
      <div>
        <el-form ref="form">
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否必修">
                <el-radio-group v-model="definition">
                  <el-radio label="1">必修</el-radio>
                  <el-radio label="2">选修</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请输入可获得学时">
                <el-input-number v-model="getReward" :controls="false" :min="0" placeholder="请输入可获得学时" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-collapse-transition>
        <section class="main-search merge">
          <span class="svg-container">
            <svg-icon icon-class="search" />
          </span>
          <el-input v-model.trim="listQuery.name" placeholder="输入名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
          <section class="search-btn">
            <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
          </section>
        </section>
      </el-collapse-transition>
      <v-table-child
        :list="tableData"
        :tag="tag"
        :height="440"
        :selection="selection"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :source-arr="['subjectImgUrl', 'courseImgUrl','imgIdUrl']"
        :is-child="isChild"
        @selectSub="handleSelect"
        @handleSelectionChange="handleSelectionChange"
      />
    </div>
    <div class="page">
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { queryPageReurcit } from '../api'
import Pagination from '@/components/Pagination'
import VTableChild from './Table'
export default {
  name: 'ChooseCourse',
  components: { Pagination, VTableChild },
  props: {
    courseClassify: {
      type: String,
      default: ''
    },
    isChild: {
      type: Boolean,
      default: false
    },
    tag: {
      // 表头
      type: String,
      default: ''
    },
    urlType: {
      // 资源分类
      type: String,
      default: ''
    },
    selection: {
      // 是否允许选择
      type: Boolean,
      default: true
    },
    areaId: {
      // 培训班id
      type: String,
      default: ''
    },
    isGet: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      listLoading: false,
      definition: '1',
      getReward: '0',
      total: 0,
      tableData: [],
      listQuery: {
        page: 1,
        limit: 10
      },
      multipleSelection: []
    }
  },
  computed: {},
  watch: {
    /* urlType(newVal) {
      this.$nextTick(() => {
        this.getList()
      })
      true
      console.log(newVal)
    }*/
  },
  created() {
    // if (this.isGet) {
    // this.getList()
    // }
  },
  methods: {
    handleSelect(info) {
      this.$emit('select', { arr: info, getReward: this.getReward, definition: this.definition })
    },
    handleSelectionChange(arr) {
      this.$emit('selectMany', { arr, getReward: this.getReward, definition: this.definition })
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        areaId: this.areaId,
        type: this.urlType,
        selectType: '1',
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      return new Promise((resolve, reject) => {
        queryPageReurcit(query)
          .then((response) => {
            this.tableData = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },
    handleRegionList(row) {
      const arr = []
      if (row.areaProvinceName) {
        arr.push(row.areaProvinceName)
      }
      if (row.areaCityName) {
        arr.push(row.areaCityName)
      }
      if (row.areaCountyName) {
        arr.push(row.areaCountyName)
      }
      return arr.join('/')
    },
    filtercolumn(scope, item) {
      if (item === 'nd') {
        return scope.row['param2']
      } else if (item === 'EachDistrict') {
        return this.handleRegionList(scope.row)
      }
      return scope.row[item]
    }
  }
}
</script>

<style type="text/css" lang="scss">
.choose-course {
  height: 550px;
  display: flex;
  flex-direction: column;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .table-data {
    flex: 1;
  }
  .page {
    height: 50px;
  }
}
</style>
